package com.mlingdu.supercloudreader;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.mlingdu.supercloudreader.databinding.ActivityMainBinding;
import com.mlingdu.supercloudreader.databinding.NavHeaderMainBinding;
import com.mlingdu.supercloudreader.ui.book.BookFragment;
import com.mlingdu.supercloudreader.ui.gank.GankFragment;
import com.mlingdu.supercloudreader.ui.one.OneFragment;
import com.mlingdu.supercloudreader.utils.PerfectClickListener;
import com.mlingdu.supercloudreader.view.MyFragmentPageAdapter;
import com.mlingdu.supercloudreader.view.statusbar.StatusBarUtil;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,ViewPager.OnPageChangeListener{
	private ActivityMainBinding mBinding;
	private DrawerLayout drawerLayout;
	private NavigationView navView;
	private FloatingActionButton fab;
	private Toolbar toolbar;
	private FrameLayout llTitleMenu;
	private ViewPager vpContent;
	private ImageView ivTitleGank;
	private ImageView ivGankOne;
	private ImageView ivGankDou;
	NavHeaderMainBinding bind;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.activity_main);
		mBinding = DataBindingUtil.setContentView(this,R.layout.activity_main);
		initStatusView();
		initId();
		initRxBus();
		initContentFragment();
		initDrawerLayout();
		initListener();
	}
	private void initStatusView(){
		//set statusViewHeight
		ViewGroup.LayoutParams layoutParams = mBinding.include.viewStatus.getLayoutParams();
		layoutParams.height = StatusBarUtil.getStatusBarHeight(this);
		mBinding.include.viewStatus.setLayoutParams(layoutParams);

	}
	private void initId(){
		//drawer_layout nav_view toolbar ll_title_menu iv_title_gank iv_gank_one iv_gank_dou vp_content
		drawerLayout = mBinding.drawerLayout;
		navView = mBinding.navView;
		fab = mBinding.include.fab;
		toolbar = mBinding.include.toolbar;
		llTitleMenu = mBinding.include.llTitleMenu;
		vpContent = mBinding.include.vpContent;

		ivTitleGank = mBinding.include.ivTitleGank;
		ivGankOne = mBinding.include.ivGankOne;
		ivGankDou = mBinding.include.ivGankDou;

		fab.setVisibility(View.GONE);

	}
	private void initRxBus(){
		//RxBus.getDefault().
	}
	private void initContentFragment(){
		//只要写完GankFragment，Training就结束。
		ArrayList<Fragment> mFragmentList = new ArrayList<>();
		mFragmentList.add(new GankFragment());
		mFragmentList.add(new OneFragment());
		mFragmentList.add(new BookFragment());

		MyFragmentPageAdapter adapter = new MyFragmentPageAdapter(getSupportFragmentManager(),mFragmentList);
		vpContent.setAdapter(adapter);
		// 设置ViewPager最大缓存的页面个数(cpu消耗少)
		vpContent.setOffscreenPageLimit(2);
		vpContent.addOnPageChangeListener(this);
		mBinding.include.ivTitleGank.setSelected(true);
		vpContent.setCurrentItem(0);
		//设置ActionBar
		setSupportActionBar(toolbar);
		ActionBar actionBar = getSupportActionBar();
		if (actionBar != null){
			//去除默认Title显示
			actionBar.setDisplayShowTitleEnabled(false);
		}

	}
	private void initDrawerLayout(){
		navView.inflateHeaderView(R.layout.nav_header_main);
		View headerView = navView.getHeaderView(0);
		bind = DataBindingUtil.bind(headerView);

		bind.ivAvatar.setOnClickListener(this);
		bind.llNavExit.setOnClickListener(this);

		bind.llNavAbout.setOnClickListener(listener);
		bind.llNavDeedBack.setOnClickListener(listener);
		bind.llNavHomepage.setOnClickListener(listener);
		bind.llNavLogin.setOnClickListener(listener);
		bind.llNavScanDownload.setOnClickListener(listener);
	}
	private void initListener(){
		//fab ivTitleGank ivGankOne ivGankDou clickable
		fab.setOnClickListener(this);
		ivTitleGank.setOnClickListener(this);
		ivGankOne.setOnClickListener(this);
		ivGankDou.setOnClickListener(this);
		llTitleMenu.setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()){
			case R.id.ll_title_menu:
				mBinding.drawerLayout.openDrawer(GravityCompat.START);
				break;
			case R.id.iv_gank_dou:

				break;
			case R.id.iv_gank_one:

				break;
			case R.id.iv_title_gank:

				break;
			case R.id.ll_nav_exit:
				break;
			case R.id.iv_avatar:
				break;
			default:
				break;
		}
	}
	private PerfectClickListener listener = new PerfectClickListener() {
		@Override
		protected void onNoDoubleClick(final View v) {
			mBinding.drawerLayout.closeDrawer(GravityCompat.START);
			mBinding.drawerLayout.postDelayed(new Runnable() {
				@Override
				public void run() {
					switch (v.getId()){
						case R.id.ll_nav_login:

							break;
						case R.id.ll_nav_deed_back:

							break;
						case R.id.ll_nav_scan_download:

							break;
						case R.id.ll_nav_about:

							break;
						case R.id.ll_nav_homepage:

							break;
						default:
							break;
					}
				}
			}, 260);
		}
	};

	@Override
	public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

	}

	@Override
	public void onPageSelected(int position) {
		switch (position) {
			case 0:
				ivTitleGank.setSelected(true);
				ivGankOne.setSelected(false);
				ivGankDou.setSelected(false);
				break;
			case 1:
				ivTitleGank.setSelected(true);
				ivGankOne.setSelected(false);
				ivGankDou.setSelected(false);
				break;
			case 2:
				ivTitleGank.setSelected(true);
				ivGankOne.setSelected(false);
				ivGankDou.setSelected(false);
				break;
		}
	}

	@Override
	public void onPageScrollStateChanged(int state) {

	}

}
